package com.simplystupid.expensestracking;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

public class MyExpensesProvider extends ContentProvider {

	public static final String TABLE_NAME = "expenses";

	public static final String DESCRIPTION = "DESCRIPTION";
	public static final String AMOUNT = "AMOUNT";
	public static final String EXPENSES_DATE = "EXPENSES_DATE";
	
	private MyExpensesDatabaseHelper dbHelper;
	private static Map<String, String> projectionMap;
	
	static {
		projectionMap = new HashMap<String, String>();
		projectionMap.put(BaseColumns._ID, BaseColumns._ID);
		projectionMap.put(DESCRIPTION, DESCRIPTION);
		projectionMap.put(AMOUNT, AMOUNT);
		projectionMap.put(EXPENSES_DATE, EXPENSES_DATE);
	}


	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		try {
			db.insertOrThrow(TABLE_NAME, null, values);
		} catch (Exception e) {
			Log.e("PROVIDER", e.getMessage());
		}
		
		return null;
	}

	@Override
	public boolean onCreate() {
		try {
			dbHelper = new MyExpensesDatabaseHelper(getContext());
		} catch (Exception e) {
			Log.e("PROVIDER", "Instantiate database helper failed.");
			return false;
		}
		
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Log.d("QUERY", "uri: " + uri);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
//		Cursor c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(TABLE_NAME);
        qb.setProjectionMap(projectionMap);
		Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	private static class MyExpensesDatabaseHelper extends SQLiteOpenHelper {

		public MyExpensesDatabaseHelper(Context context) {
			super(context, "expenses_tracking.db", null, R.string.version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql = "CREATE TABLE " + TABLE_NAME + " ( " + 
					BaseColumns._ID + " INTEGER PRIMARY KEY, " +
					DESCRIPTION + " TEXT, " +
					AMOUNT + " DOUBLE, " + 
					EXPENSES_DATE + " INTEGER " +
					");";
			Log.i("INFO", "Run create table SQL: " + sql);
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("WARN", "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
		
	}
}
